package com.hqq.util;

import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import org.slf4j.LoggerFactory;

import java.util.LinkedList;
import java.util.List;

/**
 * InMemoryAppender
 * Logger Factory
 * Created by heqianqian on 2017/6/14.
 */
public class InMemoryAppender extends AppenderBase<ILoggingEvent> {
    private List<ILoggingEvent> loggingEventList = new LinkedList<>();

    public InMemoryAppender() {
        ((Logger) LoggerFactory.getLogger("root")).addAppender(this);
        start();
    }

    public InMemoryAppender(Class clazz) {
        ((Logger) LoggerFactory.getLogger(clazz)).addAppender(this);
        start();
    }

    @Override
    protected void append(ILoggingEvent iLoggingEvent) {
        loggingEventList.add(iLoggingEvent);
    }

    public String getLastMessage() {
        return loggingEventList.get(loggingEventList.size() - 1).getFormattedMessage();
    }

    public int getLogSize() {
        return loggingEventList.size();
    }
}
